/*
 * Copyright (c) 2020-2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

 #include <stdio.h>
 #include <unistd.h>
 #include <string.h>
 #include "ohos_init.h"
 #include "cmsis_os2.h"
 #include "hal_bsp_ssd1306.h"
 #include "hal_bsp_sht20.h"
 
 
 void oled_task(void)
 {
     uint8_t displayBuff[20] = {0};
     uint8_t hour = 14, min = 48, sec = 0;
     float temperature = 0, humidity = 0;
     char temp_str[10], hum_str[10];
     SSD1306_Init(); // OLED 显示屏初始化
     SSD1306_CLS(); // 清屏
     SSD1306_ShowStr(0, 3, (uint8_t *)"2025-04-10", 16);
     
     while(1)
     {
         sec++;
         if(sec > 59)
         {
             sec = 0;
             min++;
         }
         if(min > 59)
         {
             min = 0;
             hour++;
         }
         if(hour > 23)
         {
             hour = 0;
         }
         SHT20_ReadData(&temperature, &humidity);
        
         memset(displayBuff, 0, sizeof(displayBuff));//清除displayBuff中字符串
         memset(temp_str, 0, sizeof(temp_str));
         memset(hum_str, 0, sizeof(hum_str));

         sprintf((char*)displayBuff,"%02d:%02d:%02d", hour, min, sec);
         snprintf(temp_str, sizeof(temp_str), "temp:%.1f", temperature);
        snprintf(hum_str, sizeof(hum_str), "hum:%.1f", humidity);
        
         SSD1306_ShowStr(0, 2, (uint8_t *)displayBuff, 16);
         SSD1306_ShowStr(0, 0, (uint8_t *)temp_str, 16);
        SSD1306_ShowStr(64, 0, (uint8_t *)hum_str, 16);
         sleep(1); 
   }
 }
 
 static void oled_i2c(void)
 {
     osThreadAttr_t options;
     options.name = "oled_task";
     options.attr_bits = 0;
     options.cb_mem = NULL;
     options.cb_size = 0;
     options.stack_mem = NULL;
     options.stack_size = 1024;
     options.priority = osPriorityNormal;
 
     osThreadId_t Task1_ID; 
     Task1_ID = osThreadNew((osThreadFunc_t)oled_task, NULL, &options);
     if(Task1_ID != NULL)
     {
         printf("ID = %d, Create Task1_ID is OK!", Task1_ID);
     }
 }
 
 APP_FEATURE_INIT(oled_i2c);